package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DishService dishService;

    /**
     * 添加菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody DishDto dishDto) {

        int row = dishService.add(dishDto);

        if (row > 0) {

            redisTemplate.delete("DISH_"+dishDto.getCategoryId());

            return R.success("添加菜品成功!");

        }

        return R.error("添加菜品失败!");

    }

    /**
     * 分页查询菜品
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findPage(int page, int pageSize, String name) {

        try {
            IPage<DishDto> dtoIPage = dishService.findPage(page, pageSize, name);

            return R.success(dtoIPage);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败!");
        }

    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id) {
        DishDto dto = dishService.findById(id);
        return R.success(dto);
    }

    /**
     * 修改菜单
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto) {

        int row = dishService.update(dishDto);

        if (row > 0) {

            redisTemplate.delete("DISH_"+dishDto.getCategoryId());

            return R.success("修改菜品成功!");

        }

        return R.error("修改菜品失败!");
    }

    /**
     * 回显数据
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R list(Long categoryId, Integer status) {

        List<DishDto> list = dishService.list(categoryId, status);

        if (list != null) {

            return R.success(list);
        }

        return R.error("连接异常!");
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        int row = dishService.delete(ids);
        if (row == -1) {
            return R.error("菜品处于起售状态,不能删除!");
        }

        if (row == 0) {
            return R.error("删除菜品失败!");
        }
        Set keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);
        return R.success("删除菜品成功!");
    }

    /**
     * 修改菜品成停售状态
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R stop(@RequestParam List<Long> ids) {

        int row = dishService.updateStop(ids);

        if (row > 0) {

            return R.success("操作成功!");

        }

        return R.error("操作失败!");
    }

    /**
     * 修改菜品成起售状态
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R start(@RequestParam List<Long> ids) {

        int row = dishService.updateStart(ids);

        if (row > 0) {

            return R.success("操作成功!");

        }

        return R.error("操作失败!");
    }

}
